<?php
require_once('qtbclient/qtb_board.php');
require_once('qtbclient/qtb_topic.php');
require_once('qtbclient/qtb_message.php');

class TBProxy
{
    const DEFAULT_ORDER_FIELD = 'id';
    const DEFAULT_ORDER_DIR = 'desc';
    const CLICK_INCR_FLAG = 1;
    const VISIBLE_FLAG = true;
    const QUERY_TOPIC_BY_ID = 1;
    const QUERY_TOPIC_BY_KEY = 2;

    private $_boardKey;
    
    static public function getChatIns()
    {/*{{{*/
        static $ins;
        if (false == $ins instanceof self)
        {
            $ins = new self(Constants::XZT_BOARD_KEY);
        }
        return $ins;
    }/*}}}*/

    private function __construct($app)
    {/*{{{*/
        $this->_boardSvc = new QTBCBoard();
        $this->_topicSvc = new QTBCTopic();
        $this->_messageSvc = new QTBCMessage();
        $this->_boardKey = $app;
    }/*}}}*/

    public function addBoard($board)
    {/*{{{*/
        return $this->_boardSvc->add($this->_boardKey, $board);
    }/*}}}*/
    
    public function getBoardByKey()
    {/*{{{*/
        return $this->_boardSvc->get_by_key($this->_boardKey);
    }/*}}}*/

    public function createMessage($topicID, $message)
    {/*{{{*/
        return $this->_messageSvc->add($this->_boardKey, $topicID, $message);
    }/*}}}*/

    public function deleteMessage($topicID, $messageIDArr)
    {/*{{{*/
        return $this->_messageSvc->del_messages($this->_boardKey, $topicID, $messageIDArr);
    }/*}}}*/
    
    public function getMessagesByTopicKey($topicKey, $start, $count)
    {/*{{{*/
        $options['click_add']	= self::CLICK_INCR_FLAG;
        $options['order']		= self::DEFAULT_ORDER_FIELD;
        $options['order_dir']	= self::DEFAULT_ORDER_DIR;
        $options['visible']     = self::VISIBLE_FLAG;
        
        return $this->_messageSvc->get_messages_by_topic_key($this->_boardKey, $topicKey, $start, $count, $options);
    }/*}}}*/
    
    public function getMessagesByID($topicKey, $chatID, $opts)
    {/*{{{*/
        return $this->_messageSvc->get_message_by_id($this->_boardKey, $topicKey, $chatID, $opts);
    }/*}}}*/
    
    public function getMessageNumByTopicKeys($topicKeys)
    {/*{{{*/
        $topics = array();
        $topics[$this->_boardKey] = $topicKeys;
        return $this->_messageSvc->get_nums($topics);
    }/*}}}*/
    
    public function getTopicByID($topicID)
    {/*{{{*/
        return $this->_topicSvc->get_topic_by($this->_boardKey, $topicID, self::QUERY_TOPIC_BY_ID);
    }/*}}}*/
    
    public function getTopicByKey($topicKey)
    {/*{{{*/
        return $this->_topicSvc->get_topic_by($this->_boardKey, $topicKey, self::QUERY_TOPIC_BY_KEY);
    }/*}}}*/
}
